<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        @include('common.meta')
        <meta name="csrf-token" content="{{ csrf_token() }}">
        @include('common.script')
    </head>
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form">
{{--                    <input type="hidden" name="_token" value="{{ csrf_token() }}" />--}}
                    <div class="layui-form-item">
                        <label for="L_email" class="layui-form-label">
                            <span class="x-red">*</span>一级分类</label>
                        <div class="layui-input-inline">
                            <select name="cate_pid" lay-filter="aihao">
                                @foreach($cate as $v)
                                    <option value="{{ $v->cate_id }}">{{{ $v->cate_name }}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="L_email" class="layui-form-label">
                            <span class="x-red">*</span>分类名称</label>
                        <div class="layui-input-inline">{{--lay-verify="email"--}}
                            <input type="text" lay-verify="cate_name" id="L_email" name="cate_name" required="" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="L_username" class="layui-form-label">
                            <span class="x-red">*</span>分类标题</label>
                        <div class="layui-input-inline">
                            <input type="text" name="cate_title" required="" lay-verify="cate_title" autocomplete="off" class="layui-input"></div>
                    </div>
                    <div class="layui-form-item">
                        <label for="L_username" class="layui-form-label">
                            <span class="x-red">*</span>分类图标</label>
                        <div class="layui-input-inline">
                            <img src="" id="uploadImg" />
                            <button type="button" class="layui-btn" id="test1">
                                <i class="layui-icon">&#xe67c;</i>上传图片
                            </button>
                            <button type="button" class="layui-btn" id="test2">
                                <i class="layui-icon">&#xe67c;</i>下载图片
                            </button>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="L_username" class="layui-form-label">
                            <span class="x-red">*</span>分类排序</label>
                        <div class="layui-input-inline">
                            <input type="text" name="cate_order" required="" lay-verify="cate_order" autocomplete="off" class="layui-input"></div>
                    </div>
                    <div class="layui-form-item">
                        <label for="L_repass" class="layui-form-label"></label>
                        <button class="layui-btn" lay-filter="add" lay-submit="">增加</button></div>
                </form>
            </div>
        </div>
        <script>layui.use(['form', 'layer','jquery','upload'],
            function() {
                $ = layui.jquery;
                var form = layui.form,
                layer = layui.layer,
                upload = layui.upload;
                var qiniu;
                //分类图标上传
                var uploadInst = upload.render({
                    elem: '#test1' //绑定元素
                    ,url: '/admin/cate/upload' //上传接口
                    ,headers:{
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                    ,done: function(res){
                        //上传完毕回调
                        if(res.code == 1){
                            // 正常上传
                            // $('#uploadImg').attr('src','/uploads/' + res.message)
                           // 七牛
                            $('#uploadImg').attr('src', res.data.urlImage)
                            qiniu = res.data.qiniu
                        }else {
                            layer.msg(res.message,{icon:5})
                        }
                    }
                    ,error: function(){
                        //请求异常回调
                    }
                });

                // 下载资源
                $('#test2').click(function (){
                    window.location.href = "http://www.blog.com/admin/cate/download/"+qiniu;
                })

                //自定义验证规则
                form.verify({
                    cate_name: function(value) {
                        if (value.length < 1) {
                            return '分类昵称至少得1个字符啊';
                        }
                    },
                    cate_title: function(value) {
                        if (value.length < 1) {
                            return '分类标题至少得1个字符啊';
                        }
                    },
                    cate_order: function(value) {
                        if (value.length < 1) {
                            return '分类排序至少得1个字符啊';
                        }
                    },
                });

                //监听提交
                form.on('submit(add)',
                function(data) {
                    //发异步，把数据提交给php
                    $.ajax({
                        url:'/admin/cate',
                        type:'POST',
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        dataType:'json',
                        data:data.field,
                        success:function (res){
                            // 判断
                            if(res.code == 1){
                                layer.msg(res.message,{icon:1},function (){
                                    parent.location.reload(true)
                                })
                            }else {
                                layer.msg(res.message,{icon:5})
                            }
                        },
                        error:function (res){
                        }
                    });
                    return false;
                });

            });</script>
        <script>var _hmt = _hmt || []; (function() {
                var hm = document.createElement("script");
                hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
                var s = document.getElementsByTagName("script")[0];
                s.parentNode.insertBefore(hm, s);
            })();</script>
    </body>

</html>